#pragma once

#include "imconfig.h"
#include "imgui.h"
#include "imgui_impl_dx11.h"
#include "imgui_impl_win32.h"
#include "imgui_internal.h"
#include "imstb_rectpack.h"
#include "imstb_textedit.h"
#include "imstb_truetype.h"
#include "Font.h"

//#include <time.h>
#include <string.h>
//#include <iostream>
#include <tchar.h>
#include <future>
#include <mutex>
#include <d3d11.h>
#pragma comment(lib,"d3d11.lib")
#pragma warning( disable : 4996 )

// 项目专用函数库
#include "GetEnvironmentCommon.h"





void CMD_Python_CDE_A_1(float& ScalingRatio, bool& Python_CDE_A_1, bool& Python_CDE_B_0, bool& Python_UnZip_Wait,
    bool& Python_CDE_CF_1, bool& Python_CDE_CF_0, std::mutex& PythonMutex, bool& Python_In_UnZip_0,
    bool& Python_CDE_B_3, bool& Python_CDE_B_4) {
    static bool Python_WinPos2 = true;//用于初始化窗口位置
    int Screen_Width{ GetSystemMetrics(SM_CXSCREEN) };//获取显示器的宽
    int Screen_Heigth{ GetSystemMetrics(SM_CYSCREEN) };//获取显示器的高

    // 初始化窗口
    if (Python_WinPos2) {
        ImGui::SetNextWindowPos({ float(Screen_Width - 249 * ScalingRatio) / 2,float(Screen_Heigth - 86 * ScalingRatio) / 2 });
        Python_WinPos2 = false;//初始化完毕
    }

    ImGui::Begin("Python 安装提示", &Python_CDE_A_1, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoDocking);//开始绘制窗口
    ImGui::Text("是否确认安装 Python 环境？");
    ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0.117f, 0.655f, 0.137f, 1.0f));
    ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(0.117f, 0.555f, 0.137f, 1.0f));
    ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(0.117f, 0.455f, 0.137f, 1.0f));
    if (ImGui::Button("安装")) {
        Python_CDE_A_1 = false;
        int Python_CDE_B = CheckDevEnvironmentB("Python313.zip", "EnvironmentPackage", ".\\EnvironmentPackage\\Python313.zip", "abc23cfb6b4633f51bdbbf07cb223421");
        if (Python_CDE_B == 0)
            Python_CDE_B_0 = true;
        else if (Python_CDE_B == 1) {
            // 打开等待框
            Python_UnZip_Wait = true;
            // 启动新线程处理解压和配置
            std::thread([&]() {  // 使用 [&] 捕获上下文变量（确保变量生命周期）
                // 解压 Python
                bool Python_InUnZip = getDevEnvironment(1, ".\\EnvironmentPackage\\Python313.zip");
                // 根据解压结果设置环境变量和状态
                bool InSetEnv = false;
                if (Python_InUnZip) {
                    InSetEnv = OrderlinessSetEnvironmentVariables_2(1, L"Python313\\", L"Python313\\Scripts\\");
                }
                // 关闭等待框
                Python_UnZip_Wait = false;
                // 使用互斥锁保护共享变量
                {
                    std::lock_guard<std::mutex> lock(PythonMutex);
                    if (Python_InUnZip) {
                        if (InSetEnv) {
                            Python_CDE_CF_1 = true;  // 解压且配置成功
                        }
                        else {
                            Python_CDE_CF_0 = true;  // 解压成功但配置失败
                        }
                    }
                    else {
                        Python_In_UnZip_0 = true;    // 解压失败
                    }
                }
                }).detach();  // 分离线程，使其在后台运行
        }
        else if (Python_CDE_B == 2) {
            // 打开等待框
            Python_UnZip_Wait = true;
            // 启动新线程处理解压和配置
            std::thread([&]() {  // 使用 [&] 捕获上下文变量（确保变量生命周期）
                // 解压 Python
                bool Python_InUnZip = getDevEnvironment(2, ".\\EnvironmentPackage\\Python313.zip");
                // 根据解压结果设置环境变量和状态
                bool InSetEnv = false;
                if (Python_InUnZip) {
                    InSetEnv = OrderlinessSetEnvironmentVariables_2(2, L"Python313\\", L"Python313\\Scripts\\");
                }
                // 关闭等待框
                Python_UnZip_Wait = false;
                // 使用互斥锁保护共享变量
                {
                    std::lock_guard<std::mutex> lock(PythonMutex);
                    if (Python_InUnZip) {
                        if (InSetEnv) {
                            Python_CDE_CF_1 = true;  // 解压且配置成功
                        }
                        else {
                            Python_CDE_CF_0 = true;  // 解压成功但配置失败
                        }
                    }
                    else {
                        Python_In_UnZip_0 = true;    // 解压失败
                    }
                }
                }).detach();  // 分离线程，使其在后台运行
        }
        else if (Python_CDE_B == 3)
            Python_CDE_B_3 = true;
        else/* if (Python_CDE_B == 4)*/
            Python_CDE_B_4 = true;
    }
    ImGui::PopStyleColor(3);
    ImGui::SameLine();
    ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0.75f, 0.1f, 0.1f, 1.0f));
    ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(0.75f, 0.25f, 0.25f, 1.0f));
    ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(0.75f, 0.4f, 0.4f, 1.0f));
    if (ImGui::Button("关闭"))
        Python_CDE_A_1 = false;
    ImGui::PopStyleColor(3);
    ImGui::End();
}


void CMD_Python_CDE_A_2(float& ScalingRatio, bool& Python_CDE_A_2) {
    static bool Python_WinPos2 = true;//用于初始化窗口位置
    int Screen_Width{ GetSystemMetrics(SM_CXSCREEN) };//获取显示器的宽
    int Screen_Heigth{ GetSystemMetrics(SM_CYSCREEN) };//获取显示器的高

    // 初始化窗口
    if (Python_WinPos2) {
        ImGui::SetNextWindowPos({ float(Screen_Width - 341 * ScalingRatio) / 2,float(Screen_Heigth - 86 * ScalingRatio) / 2 });
        Python_WinPos2 = false;//初始化完毕
    }

    ImGui::Begin("Python 安装提示", &Python_CDE_A_2, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoDocking);//开始绘制窗口
    ImGui::Text("相同版本的环境已安装，无法继续安装。");
    ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0.75f, 0.1f, 0.1f, 1.0f));
    ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(0.75f, 0.25f, 0.25f, 1.0f));
    ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(0.75f, 0.4f, 0.4f, 1.0f));
    if (ImGui::Button("取消安装"))
        Python_CDE_A_2 = false;
    ImGui::PopStyleColor(3);
    ImGui::End();
}


void CMD_Python_CDE_A_3(float& ScalingRatio, bool& Python_CDE_A_3) {
    static bool Python_WinPos2 = true;//用于初始化窗口位置
    int Screen_Width{ GetSystemMetrics(SM_CXSCREEN) };//获取显示器的宽
    int Screen_Heigth{ GetSystemMetrics(SM_CYSCREEN) };//获取显示器的高

    // 初始化窗口
    if (Python_WinPos2) {
        ImGui::SetNextWindowPos({ float(Screen_Width - 341 * ScalingRatio) / 2,float(Screen_Heigth - 86 * ScalingRatio) / 2 });
        Python_WinPos2 = false;//初始化完毕
    }

    ImGui::Begin("Python 安装提示", &Python_CDE_A_3, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoDocking);//开始绘制窗口
    ImGui::Text("不同版本的环境已安装，无法继续安装。");
    ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0.75f, 0.1f, 0.1f, 1.0f));
    ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(0.75f, 0.25f, 0.25f, 1.0f));
    ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(0.75f, 0.4f, 0.4f, 1.0f));
    if (ImGui::Button("取消安装"))
        Python_CDE_A_3 = false;
    ImGui::PopStyleColor(3);
    ImGui::End();
}


void CMD_Python_CDE_A_4(float& ScalingRatio, bool& Python_CDE_A_4) {
    static bool Python_WinPos2 = true;//用于初始化窗口位置
    int Screen_Width{ GetSystemMetrics(SM_CXSCREEN) };//获取显示器的宽
    int Screen_Heigth{ GetSystemMetrics(SM_CYSCREEN) };//获取显示器的高

    // 初始化窗口
    if (Python_WinPos2) {
        ImGui::SetNextWindowPos({ float(Screen_Width - 214 * ScalingRatio) / 2,float(Screen_Heigth - 86 * ScalingRatio) / 2 });
        Python_WinPos2 = false;//初始化完毕
    }

    ImGui::Begin("Python 安装提示", &Python_CDE_A_4, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoDocking);//开始绘制窗口
    ImGui::Text("环境有无情况判断异常！");
    ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0.75f, 0.1f, 0.1f, 1.0f));
    ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(0.75f, 0.25f, 0.25f, 1.0f));
    ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(0.75f, 0.4f, 0.4f, 1.0f));
    if (ImGui::Button("取消安装"))
        Python_CDE_A_4 = false;
    ImGui::PopStyleColor(3);
    ImGui::End();
}


void CMD_Python_CDE_B_0(float& ScalingRatio, bool& Python_CDE_B_0) {
    static bool Python_WinPos3 = true;//用于初始化窗口位置
    int Screen_Width{ GetSystemMetrics(SM_CXSCREEN) };//获取显示器的宽
    int Screen_Heigth{ GetSystemMetrics(SM_CYSCREEN) };//获取显示器的高

    // 初始化窗口
    if (Python_WinPos3) {
        ImGui::SetNextWindowPos({ float(Screen_Width - 192 * ScalingRatio) / 2,float(Screen_Heigth - 86 * ScalingRatio) / 2 });
        Python_WinPos3 = false;//初始化完毕
    }

    ImGui::Begin("Python 安装提示", &Python_CDE_B_0, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoDocking);//开始绘制窗口
    ImGui::Text("C / D 存储空间不足。");
    ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0.75f, 0.1f, 0.1f, 1.0f));
    ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(0.75f, 0.25f, 0.25f, 1.0f));
    ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(0.75f, 0.4f, 0.4f, 1.0f));
    if (ImGui::Button("取消安装"))
        Python_CDE_B_0 = false;
    ImGui::PopStyleColor(3);
    ImGui::End();
}


void CMD_Python_CDE_B_3(float& ScalingRatio, bool& Python_CDE_B_3) {
    static bool Python_WinPos3 = true;//用于初始化窗口位置
    int Screen_Width{ GetSystemMetrics(SM_CXSCREEN) };//获取显示器的宽
    int Screen_Heigth{ GetSystemMetrics(SM_CYSCREEN) };//获取显示器的高

    // 初始化窗口
    if (Python_WinPos3) {
        ImGui::SetNextWindowPos({ float(Screen_Width - 323 * ScalingRatio) / 2,float(Screen_Heigth - 86 * ScalingRatio) / 2 });
        Python_WinPos3 = false;//初始化完毕
    }

    ImGui::Begin("Python 安装提示", &Python_CDE_B_3, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoDocking);//开始绘制窗口
    ImGui::Text("本程序提供的环境文件可能遭到修改。");
    ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0.75f, 0.1f, 0.1f, 1.0f));
    ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(0.75f, 0.25f, 0.25f, 1.0f));
    ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(0.75f, 0.4f, 0.4f, 1.0f));
    if (ImGui::Button("取消安装"))
        Python_CDE_B_3 = false;
    ImGui::PopStyleColor(3);
    ImGui::End();
}


void CMD_Python_CDE_B_4(float& ScalingRatio, bool& Python_CDE_B_4) {
    static bool Python_WinPos3 = true;//用于初始化窗口位置
    int Screen_Width{ GetSystemMetrics(SM_CXSCREEN) };//获取显示器的宽
    int Screen_Heigth{ GetSystemMetrics(SM_CYSCREEN) };//获取显示器的高

    // 初始化窗口
    if (Python_WinPos3) {
        ImGui::SetNextWindowPos({ float(Screen_Width - 196 * ScalingRatio) / 2,float(Screen_Heigth - 86 * ScalingRatio) / 2 });
        Python_WinPos3 = false;//初始化完毕
    }

    ImGui::Begin("Python 安装提示", &Python_CDE_B_4, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoDocking);//开始绘制窗口
    ImGui::Text("环境安装文件不存在。");
    ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0.75f, 0.1f, 0.1f, 1.0f));
    ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(0.75f, 0.25f, 0.25f, 1.0f));
    ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(0.75f, 0.4f, 0.4f, 1.0f));
    if (ImGui::Button("取消安装"))
        Python_CDE_B_4 = false;
    ImGui::PopStyleColor(3);
    ImGui::End();
}


void CMD_Python_In_UnZip_0(float& ScalingRatio, bool& Python_In_UnZip_0) {
    static bool Python_WinPos4 = true;//用于初始化窗口位置
    int Screen_Width{ GetSystemMetrics(SM_CXSCREEN) };//获取显示器的宽
    int Screen_Heigth{ GetSystemMetrics(SM_CYSCREEN) };//获取显示器的高

    // 初始化窗口
    if (Python_WinPos4) {
        ImGui::SetNextWindowPos({ float(Screen_Width - 178 * ScalingRatio) / 2,float(Screen_Heigth - 86 * ScalingRatio) / 2 });
        Python_WinPos4 = false;//初始化完毕
    }

    ImGui::Begin("Python 安装提示", &Python_In_UnZip_0, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoDocking);//开始绘制窗口
    ImGui::Text("环境文件解压失败。");
    ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0.75f, 0.1f, 0.1f, 1.0f));
    ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(0.75f, 0.25f, 0.25f, 1.0f));
    ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(0.75f, 0.4f, 0.4f, 1.0f));
    if (ImGui::Button("取消安装"))
        Python_In_UnZip_0 = false;
    ImGui::PopStyleColor(3);
    ImGui::End();
}


void CMD_Python_CDE_CF_0(float& ScalingRatio, bool& Python_CDE_CF_0) {
    static bool Python_WinPos5 = true;//用于初始化窗口位置
    int Screen_Width{ GetSystemMetrics(SM_CXSCREEN) };//获取显示器的宽
    int Screen_Heigth{ GetSystemMetrics(SM_CYSCREEN) };//获取显示器的高

    // 初始化窗口
    if (Python_WinPos5) {
        ImGui::SetNextWindowPos({ float(Screen_Width - 305 * ScalingRatio) / 2,float(Screen_Heigth - 86 * ScalingRatio) / 2 });
        Python_WinPos5 = false;//初始化完毕
    }

    ImGui::Begin("Python 安装提示", &Python_CDE_CF_0, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoDocking);//开始绘制窗口
    ImGui::Text("系统环境变量设置失败，安装失败。");
    ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0.75f, 0.1f, 0.1f, 1.0f));
    ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(0.75f, 0.25f, 0.25f, 1.0f));
    ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(0.75f, 0.4f, 0.4f, 1.0f));
    if (ImGui::Button("取消安装"))
        Python_CDE_CF_0 = false;
    ImGui::PopStyleColor(3);
    ImGui::End();
}


void CMD_Python_CDE_CF_1(float& ScalingRatio, bool& Python_CDE_CF_1) {
    static bool Python_WinPos5 = true;//用于初始化窗口位置
    int Screen_Width{ GetSystemMetrics(SM_CXSCREEN) };//获取显示器的宽
    int Screen_Heigth{ GetSystemMetrics(SM_CYSCREEN) };//获取显示器的高

    // 初始化窗口
    if (Python_WinPos5) {
        ImGui::SetNextWindowPos({ float(Screen_Width - 213 * ScalingRatio) / 2,float(Screen_Heigth - 86 * ScalingRatio) / 2 });
        Python_WinPos5 = false;//初始化完毕
    }

    ImGui::Begin("Python 安装提示", &Python_CDE_CF_1, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoDocking);//开始绘制窗口
    ImGui::Text("成功安装 Python 环境！");
    ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0.117f, 0.655f, 0.137f, 1.0f));
    ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(0.117f, 0.555f, 0.137f, 1.0f));
    ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(0.117f, 0.455f, 0.137f, 1.0f));
    if (ImGui::Button("好的"))
        Python_CDE_CF_1 = false;
    ImGui::PopStyleColor(3);
    ImGui::End();
}


void CMD_Python_UnZip_Wait(float& ScalingRatio) {
    static bool Python_UnZip_Wait_WinPos2 = true;//用于初始化窗口位置
    int Screen_Width{ GetSystemMetrics(SM_CXSCREEN) };//获取显示器的宽
    int Screen_Heigth{ GetSystemMetrics(SM_CYSCREEN) };//获取显示器的高
    if (Python_UnZip_Wait_WinPos2)//初始化窗口
    {
        ImGui::SetNextWindowPos({
            float(Screen_Width - 250 * ScalingRatio) / 2,
            float(Screen_Heigth - 86 * ScalingRatio) / 2
            });
        Python_UnZip_Wait_WinPos2 = false;//初始化完毕
    }
    //开始绘制窗口
    ImGui::Begin("Python - 请稍候",
        nullptr, // 禁用关闭按钮
        ImGuiWindowFlags_NoResize |
        ImGuiWindowFlags_NoSavedSettings |
        ImGuiWindowFlags_NoCollapse |
        ImGuiWindowFlags_NoDocking
    );
    ImGui::Text("环境正在安装中，稍安勿躁。");

    // 传递一个动态负值，例如：-1.0f * (float)ImGui::GetTime() 是推荐的值。
    // 如需调整动画速度，请调整此因子。
    ImGui::ProgressBar(-1.0f * (float)ImGui::GetTime(), ImVec2(0.0f, 0.0f), "安装中…");
    ImGui::End();
}